//引入puppeteer
const puppeteer = require("puppeteer");
const fs = require("fs");

//使用async/await处理异步
(async () => {
  //创建一个Browser（浏览器）实例
  const browser = await puppeteer.launch({ headless: false });
  browser.on("disconnected", () => {
    console.log("disconnected ------------");
  });
  //在浏览器中创建一个新的页面
  const page = await browser.newPage();
  //设置页面的尺寸
  await page.setViewport({
    width: 1400,
    height: 800,
  });
  //打开页面
  await page.goto("https://tools.liumingye.cn/music/#/");
  const baiduContent = await page.$("body");
  const divElement = await page.$$("div");
  // await page.$('#kw');
  await page.keyboard.type("Hello World!");
  const keyboard = await page.keyboard.down("Shift");
  console.log("keyboard: ", keyboard);
  await page.keyboard.down("Enter");
  let h1 = await page.$eval("body", (node) => {
    return node.innerText;
  });
  fs.writeFileSync("html.txt", h1);
  //页面截图（设置截图路径）
  await page.screenshot({ path: "example.png" });
  //关闭浏览器实例
  // await browser.close();
})();
